package com.quick.develop.form.application.impl;

import cn.hutool.core.util.*;
import com.quick.develop.business.vo.BusinessProcessVO;
import com.quick.develop.form.application.FormApplication;
import com.quick.develop.form.utils.FormUtils;
import com.quick.develop.form.vo.FormInstanceEntity;
import com.quick.develop.form.vo.FormQueryVO;
import com.quick.develop.form.constans.FormConstance;
import com.quick.develop.framework.common.annotation.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class FormApplicationImpl implements FormApplication {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object executeFormBusiness(BusinessProcessVO formInstance) {
        Map<String, Object> formInstanceData = formInstance.getData();
        if (Objects.nonNull(formInstanceData) && !formInstanceData.isEmpty()) {
            executeFormBusinessBeforeHooks(formInstance);
            Object primary = formInstanceData.get(FormConstance.FORM_ID);
            try {
                if (Objects.nonNull(primary)) {
                    FormUtils.handleFormServiceMethodType(formInstance.getBusinessType(), UpdateForm.class, formInstanceData);
                } else {
                    FormUtils.handleFormServiceMethodType(formInstance.getBusinessType(), CreateForm.class, formInstanceData);
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("业务处理异常:" + e.getMessage());
            }
            executeFormBusinessAfterHooks(formInstance);
            return primary;
        }
        return null;
    }

    @Override
    public Object getForm(FormQueryVO queryVO) {
        Object result;
        try {
            result = FormUtils.handleFormServiceMethodType(queryVO.getBusinessType(), DetailForm.class, queryVO.getBusinessKey());
            if (Objects.isNull(result)) {
                HashMap<Object, Object> initialFormService = new HashMap<>();
                initialFormService.put(FormConstance.FORM_BUSINESS_KEY, IdUtil.fastUUID());
                result = initialFormService;
            }
        } catch (Exception e) {
            throw new RuntimeException("未找到表单服务:" + e.getMessage());
        }
        return result;
    }

    @Override
    public void removeForm(FormQueryVO queryVO) {
        try {
            FormUtils.handleFormServiceMethodType(queryVO.getBusinessType(), DeleteForm.class, queryVO.getIds());
        } catch (Exception e) {
            throw new RuntimeException("删除表单数据异常:" + e.getMessage());
        }
    }

    @Override
    public Object getFormPage(FormQueryVO queryVO) {
        try {
            return FormUtils.handleFormServiceMethodType(queryVO.getBusinessType(), QueryTable.class, queryVO.getParams());
        } catch (Exception e) {
            throw new RuntimeException("获取表单数据异常:" + e.getMessage());
        }
    }

    public void executeFormBusinessBeforeHooks(BusinessProcessVO formInstance) {

    }

    public void executeFormBusinessAfterHooks(BusinessProcessVO formInstance) {

    }




}
